package cn.edu.nju.ws.sview.wyge.servlets;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import cn.edu.nju.ws.sview.wyge.relations.adataset.relations.Relation;

/**
 * Servlet implementation class Entity
 */
public class ChangeRoles extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ChangeRoles() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String json = null;
		response.setCharacterEncoding("utf-8");
		if (request.getParameter("data") != null) {
			json = request.getParameter("data");
			String str = URLDecoder.decode(json, "UTF-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			int id = Integer.parseInt((String) regtJSON.get("id"));
			JSONArray inarray = (JSONArray) regtJSON.get("roles");
			HashMap<String, String> vr = new HashMap<String, String>();
			for (Object o : inarray) {
				JSONObject row = (JSONObject) o;
				String v = (String) row.get("v");
				String role = (String) row.get("role");
				vr.put(v, role);
			}
			JSONObject result = new JSONObject();
			Relation rel = (Relation) request.getSession().getAttribute(
					"relation" + id);
			if(rel == null){
				result.put("result", "nosession");
				System.err.println("****Error in ChangeRoles.java! rel=null");
			}else{
				rel.setVariableRole(vr);
				result.put("result", "OK");
				request.getSession().removeAttribute("relation"+id);//========
				request.getSession().setAttribute("relation"+rel.getRelationID(), rel);//===========
				
//				String tstRoles = ((Relation)request.getSession().getAttribute("relation"+id)).getRoles().toString();
//				System.out.println("roles:"+inarray+"|"+tstRoles);
			}
			response.getWriter().print(result);
		}else if(request.getParameter("changeName") != null){
			json = request.getParameter("changeName");
			String str = URLDecoder.decode(json, "UTF-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			int id = Integer.parseInt((String) regtJSON.get("id"));
			String name = (String) regtJSON.get("name");
			
			JSONObject result = new JSONObject();
			Relation rel = (Relation) request.getSession().getAttribute(
					"relation" + id);
			if(rel == null){
				result.put("result", "nosession");
				System.err.println("****Error in ChangeRoles.java! rel=null");
			}else{
				rel.setName(name);
//				request.getSession().removeAttribute("relation"+id);//========
//				request.getSession().setAttribute("relation"+rel.getRelationID(), rel);//===========
				result.put("result", "OK");
//				String tstName = ((Relation)request.getSession().getAttribute("relation"+id)).getName();
//				System.out.println("name:"+name+"|"+tstName);
			}
			response.getWriter().print(result);
		}
	}
}
